package cn.jy.nvr.utils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;

public class DistanceUtils {
	    /**
	     * @param startLng 当前经度
	     * @param startLat  当前纬度
	     * @param endLng 目标经度
	     * @param endLat  目标纬度
	     * @return
	     * @throws Exception
	     */
	    @SuppressWarnings("unchecked")  
	    public static String twoCitysDistance(String startLng, String startLat,String endLng,String endLat) throws Exception{  
	          
	        Map<String, String> params = new LinkedHashMap<String, String>();
	        params.put("key","33016b6a84a33657eca601b0f393067b");
	        params.put("origins",startLng+","+startLat);
	        params.put("destination",endLng+","+endLat);
	        params.put("type", "1");
	        String result = HttpClientUtil.doGet("http://restapi.amap.com/v3/distance", params);
	        return result;  
	    }  
	   @SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		   String twoCitysDistance = twoCitysDistance("104.05413", "34.44213","104.17275157782","34.6160086617");
		   ObjectMapper objectMapper =new ObjectMapper();
		   Map<String,Object> readValue = objectMapper.readValue(twoCitysDistance,Map.class);
		      List<Object> list = (List<Object>) readValue.get("results");  
		      Map<String,Object> resultMap = (Map<String,Object>)list.get(0);
		      String distance = (String) resultMap.get("distance");
		      System.out.println(distance);
	}   
}
